package com.waidi.clothespad.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;

import com.waidi.clothespad.R;
import com.waidi.clothespad.adapter.ListviewData;

import java.util.LinkedList;

/**
 * Created by ll on 2019/7/5.
 */

public class MyListViewAdapter2 extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    public LinkedList<ListviewData> mData;
    private int defaultSelection = -1;

    public MyListViewAdapter2() {}

    public MyListViewAdapter2(LinkedList<ListviewData> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        //mInflater = LayoutInflater.from(mContext);
    }

    public int getSelection()
    {

        return  defaultSelection;
    }

    @Override
    public int getCount() {
        return mData.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try
        {
            ViewHolder holder = null;
            if(convertView == null)
            {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.qc_list_item_single,parent,false);
                holder = new ViewHolder();
                holder.img_icon = (ImageView) convertView.findViewById(R.id.imgtou);
                holder.txt_content0 = (TextView) convertView.findViewById(R.id.content0);
                holder.txt_content1 = (TextView) convertView.findViewById(R.id.content1);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.img_icon.setImageResource(mData.get(position).getImgId());
            holder.txt_content0.setText(mData.get(position).getContent0());
            holder.txt_content1.setText(mData.get(position).getContent1());

            if (position == defaultSelection)
            {// 选中时设置单纯颜色
               // holder.txt_content0.setTextColor("0");
                convertView.setBackgroundColor(Color.rgb(246,214,255));
            }
            else
            {
                // 未选中时设置selector
                //holder.txt_item.setTextColor(colors);
                convertView.setBackgroundColor(Color.TRANSPARENT);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return convertView;
    }

    private class ViewHolder
    {
        ImageView img_icon;
        TextView txt_content0;
        TextView txt_content1;
    }

    /**
     * @param position
     *            设置高亮状态的item
     */
    public void setSelectPosition(int position)
    {
        if (!(position < 0 || position > mData.size()))
        {
            defaultSelection = position;
            notifyDataSetChanged();
        }
    }

    public void clearSelectPosition(int position)
    {
        if (!(position < 0 || position > mData.size())) {
            defaultSelection = -1;
            notifyDataSetChanged();
        }
    }
    }